class EnemySpaceShip extends SpaceShip {
  String _movementType;
  int ascending;
  num rand;
  
  EnemySpaceShip(String movementType): super({'src':'img/trollfaceSpaceship.png'}) {
    this._movementType = movementType;
    ascending = 1;
    rand = Math.random();
  }

  void onNewParent()
  {    
    switch (this._movementType) {
      case 'TROLL':
        this.setPosition(1000, (parent.height/2).toInt());
        this.speedX = 2;
		this.speedY = 60;
        break;
      case 'BEAM':
        this.setPosition(1000, (parent.height/2).toInt());
        this.speedX = 3;
		this.speedY = 3;
        break;
    }
    
    super.onNewParent();
  }
  
  void update(int time) {
    
    if(this.destroyed)
    {
      this.destroy();
    }
    else
    {
      switch (this._movementType) {
        case 'TROLL':
          this.incX(-speedX);
          this.setY((Math.sin(getX()/20)*speedY + (100 * rand) + parent.height/2 - this.height).toInt());
          break;
          
        case 'BEAM':
          incX(-speedX);
          num height = parent.height;
          if (getY() + ascending * speedY + (100 * rand) > height/2 || getY() + ascending * speedY + (100 * rand)< 0) {
            ascending = ascending * -1;
          }
          incY(ascending * speedY);
          break;
      }
      
      if(getX() < -this.width)
      {
        this.destroy();
      }
    }
  }
  
  void collisionDetected()
  {
    this.destroyed = true;
    createExplosion();
  }
  
  void createExplosion()
  {
    Explosion explosion = new Explosion();
    explosion.setPosition(getX(), getY());    
    this.parent.addChild(explosion);
  }
  
  void destroy()
  {
    colManager.removeObject(this);
    this.parent.removeChild(this);
    this.parent = null;
  }
  
  String getType()
  {
    return "ENEMY_SHIP";
  }
  
  List<String> getCollidableTypes()
  {
    return ["BULLET", "PLAYER_SHIP"]; 
  }
}
